package com.ruoyi.chiwen.controller;

import com.alibaba.fastjson.JSONObject;
import io.rong.RongCloud;
import io.rong.messages.InfoNtfMessage;
import io.rong.messages.VoiceMessage;
import io.rong.methods.message._private.Private;
import io.rong.methods.user.User;
import io.rong.models.message.PrivateMessage;
import io.rong.models.response.ResponseResult;
import io.rong.models.response.TokenResult;
import io.rong.models.user.UserModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;

public class RYApiUtils {
    private static Logger log = LoggerFactory.getLogger(RYApiUtils.class);

    /**
     * 融云注册
     * @param appKey
     * @param appSecret
     * @param user
     * @return
     * @throws Exception
     */
    public static String getRyToken(String appKey, String appSecret, UserModel user) throws Exception {
        RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
        // 自定义 api 地址方式
        // RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret,api);
        // 使用 百度 HTTPDNS 获取最快的 IP 地址进行连接
        // BaiduHttpDNSUtil.setHostTypeIp("account_id", "secret", rongCloud.getApiHostType());

        // 设置连接超时时间
        // rongCloud.getApiHostType().setConnectTimeout(10000);
        // 设置读取超时时间
        // rongCloud.getApiHostType().setReadTimeout(10000);
        // 获取备用域名List
        // List<HostType> hosttypes = rongCloud.getApiHostListBackUp();
        // 设置连接、读取超时时间
        // for (HostType hosttype : hosttypes) {
        //     hosttype.setConnectTimeout(10000);
        //     hosttype.setReadTimeout(10000);
        // }
        User User = rongCloud.user;

        /**
         * API 文档: http://www.rongcloud.cn/docs/server_sdk_api/user/user.html#register
         *
         * 注册用户，生成用户在融云的唯一身份标识 Token
         */
        TokenResult result = User.register(user);
        System.out.println("getToken:  " + result.toString());

        return  result.toString();
    }

    private static final VoiceMessage voiceMessage = new VoiceMessage("hello", "helloExtra", 20L);

    public static String sendMessage(String appKey, String appSecret, PrivateMessage privateMessage) throws Exception {
        RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
        Private Private = rongCloud.message.msgPrivate;
        ResponseResult privateResult = Private.send(privateMessage);
        String result = privateResult.toString();
        JSONObject jsonObject = JSONObject.parseObject(result);
        log.info("-------融云返回结果："+result);
        String code = jsonObject.getString("code");
        return code;
    }

    public static void main(String[] args) {
//        try {
//            InfoNtfMessage infoNtfMessage = new InfoNtfMessage("sss", "helloExtra");
//            String[] targetIds = {"13403333333"};
//
//            PrivateMessage privateMessage = new PrivateMessage()
//                    .setSenderId("13402222222")
//                    .setTargetId(targetIds)
//                    .setObjectName(infoNtfMessage.getType())
//                    .setContent(infoNtfMessage)
//                    .setPushContent("")
//                    .setVerifyBlacklist(0)
//                    .setIsCounted(0)
//                    .setIsIncludeSender(0);
//            String result = sendMessage("vnroth0kvlm9o","jS6fFuoFOM",privateMessage);
//            System.out.println("send private message:  " + result);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }


        //获取token
        UserModel user = new UserModel();
        user.setId("11012066666");
        user.setName("帅逼");
        user.setPortrait("http://39.105.163.95:8080/liu/dianwan.png");
        try {
            getRyToken("pvxdm17jpe6vr","dyzXaKQPFQAN",user);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        token1 ： 00001   GL81tY9z0JCqdwDuR5AD5mIQxqTuWtbdRsTsB0sCgjo=@43ch.cn.rongnav.com;43ch.cn.rongcfg.com
//        token2： 00002   ZxKyPUVtfxOqdwDuR5AD5gS1d4NKA0uNyOouER7i4ZQ=@43ch.cn.rongnav.com;43ch.cn.rongcfg.com
//        token3： 17630658322   iJEz/XzOtk0LxCI+Y9AuxI7DogmP6kjMRqcjg5X/Jym4BJoG8NWGmw==@43ch.cn.rongnav.com;43ch.cn.rongcfg.com
//        token4： 13457634582   AB0iHRh+X8h68vCtDunXtY7DogmP6kjM/7O8bSfLHeO4BJoG8NWGmw==@43ch.cn.rongnav.com;43ch.cn.rongcfg.com
//        token5： 17630648527   iJEz/XzOtk0YgjjWWNfK9I7DogmP6kjM/xBKFBk6uHq4BJoG8NWGmw==@43ch.cn.rongnav.com;43ch.cn.rongcfg.com
    }
}
